using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using PropSys;
using Unity.Mathematics;

public class StDash : MovState
{
    private string _key;
    private float _pctSpd;
    private bool _isDashing;
    private int _iptInterval;
    private int _iptTimer;
    private bool _isZeroDir;
    private int IptTimer { get => _iptTimer; set => _iptTimer = math.max(0, value); }

    public StDash(Action<StType> swcSt, MainCtrl mc, string key, float pctSpd, int iptInterval) : base(swcSt, mc)
    {
        _key = key;
        _pctSpd = pctSpd;
        _iptInterval = iptInterval;
    }

    public override void Enter()
    {
        _MC.PlayAnim("Idle");
        _MC.RegVal(PropType.SPD, PropReg.PCT, _key, delegate { return _pctSpd; });
        _isDashing = true;
        _isZeroDir = false;
    }

    public override void Exit()
    {
        Debug.Log("Exit");
        _MC.UregVal(PropType.SPD, PropReg.PCT, _key);
        _isDashing = false;
    }

    public override void LogicUpd()
    {
        _MC.Move();
        if (_isZeroDir)
        {
            if (IptTimer-- <= 0) SwcSt(StType.Idle);
        }
    }

    public override void IptDir(Vector2 vec2)
    {
        _MC.GetDri(vec2);
        if (!_isDashing)
        {
            if (vec2.magnitude > 0.8) SwcSt(StType.Run);
            else if (vec2.magnitude > 0) SwcSt(StType.Walk);
        }
        if (vec2.magnitude == 0)//in case dash in the reversed dir
        {
            if (!_isZeroDir)
            {
                _isZeroDir = true;
                IptTimer = _iptInterval;
            }
        }
        else _isZeroDir = false;
    }

    public override void Blk() { SwcSt(StType.Blk); }
    public override void PfmSkill() { SwcSt(StType.Skill); }
    public override void Air() { _MC.Jump(); }
    public override void Dodge() { SwcSt(StType.Dodge); }
    public override void CancelDash() { _isDashing = false; }
}